﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using myBoard.Framework;
using System.Globalization;
using System.Windows.Threading;
using System.Reflection;

namespace CountdownCalendar
{
	/// <summary>
	/// Interaction logic for Magnet.xaml
	/// </summary>
	public partial class Magnet : IMagnet, IRequiresStorage
	{
        Storage _storage;
        Settings _settings;
        DispatcherTimer _refreshTimer;

		public Magnet()
		{
			this.InitializeComponent();
            _refreshTimer = new DispatcherTimer();
            _refreshTimer.Interval = new TimeSpan(0, 5, 0);
            _refreshTimer.Tick += new EventHandler(_refreshTimer_Tick);
            _configBt.Click += new EventHandler(_configBt_Click);
            _doneBt.Click += new RoutedEventHandler(_doneBt_Click);
            _eventDateTbx.KeyUp += new KeyEventHandler(_eventDateTbx_KeyUp);
            _eventNameTbx.KeyUp += new KeyEventHandler(_eventNameTbx_KeyUp);
            _refreshTimer.Start();
		}

        void _refreshTimer_Tick(object sender, EventArgs e)
        {
            if(_settings!=null)
                _settings.Refresh();
        }

        void _eventNameTbx_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                _doneBt_Click(sender, e);
        }

        void _eventDateTbx_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                _doneBt_Click(sender, e);
        }

        void _configBt_Click(object sender, EventArgs e)
        {
            FlipRequested(MagnetSides.Back);
        }

        void _doneBt_Click(object sender, RoutedEventArgs e)
        {
            _settings.EventName = _eventNameTbx.Text;
            
            DateTime date;
            CultureInfo enUS = new CultureInfo("en-US"); 
            bool isValid = DateTime.TryParseExact(_eventDateTbx.Text, "MM/dd/yyyy",enUS, DateTimeStyles.None, out date);
            if (isValid)
            {
                _settings.EventDate = _eventDateTbx.Text;
                FlipRequested(MagnetSides.Front);
            }
            else
            {
                MBMessageBox mbx = new MBMessageBox("The entered date is not valid.", "Invalid date");
                mbx.ShowDialog();
            }
        }

        #region IMagnet Members

        public event OnFlipRequested FlipRequested;

        public string MagnetName
        {
            get { return "Countdown"; }
        }

        public BitmapImage Icon
        {
            get
            {
                return ResourcesAccess.GetBitmapImageFromResource(Assembly.GetExecutingAssembly(), "CountdownCalendar.Images.thumbnail.png");
            }
        }

        BitmapImage _thumbnail;

        public BitmapImage Thumbnail
        {
            get
            {
                return ResourcesAccess.GetBitmapImageFromResource(Assembly.GetExecutingAssembly(), "CountdownCalendar.Images.thumbnail.png");
            }
        }

        public Panel GetFrontSide()
        {
            return _front;
        }

        public Panel GetBackSide()
        {
            return _back;
        }

        public Panel GetFlipZone()
        {
            throw new NotImplementedException();
        }

        public void Terminate()
        {
            _refreshTimer.Stop();
            _refreshTimer = null;
        }

        #endregion

        #region IRequiresStorage Members

        public void GetStorage(Storage storage)
        {   
            _storage=storage;
            _settings = Serializer.Load<Settings>(MagnetName, System.IO.Path.Combine(storage.StorageDirectory.FullName, "Settings.xml"));
            ObjectDataProvider odp = this.Resources["SettingsInstance"] as ObjectDataProvider;
            odp.ObjectInstance = _settings;
        }

        public void SaveState()
        {
            Serializer.Save(MagnetName,_settings, System.IO.Path.Combine(_storage.StorageDirectory.FullName, "Settings.xml"));
        }

        #endregion
    }
}